package co.fitstart.admin.entity.search;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.order.OrderStatus;
import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.site.Site;


public class OrderSearch implements QueryBuilder{

    private String name;
    
    private String orderCode;

    private String mobile;
    
    private OrderType orderType;
    
    private boolean experience;
    
    private boolean give;
    
    private String paymentStatus;
    
    private String status;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeStart;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeEnd;
    
    private String orderStatus;
    
    private String productName;
    
    private Site site;
    
    private List<Site> sites;
    
    private Order order;

    private Filter filter;
    
    /**
     * default constructor
     */
    public OrderSearch() {
        order = new Order("createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        List<OrderStatus> orderStatuses = OrderStatus.sameAs(status);
        
        OrderStatus thisOrderStatus = OrderStatus.aliasOf(orderStatus);
        
        PaymentStatus thisPaymentStatus = PaymentStatus.aliasOf(paymentStatus);
        
        if(filter == null || refresh) {
            
            filter =  new Filter().addEquals("disabled", false)
                                  .addEquals("orderType", orderType)
                                  .addEquals("paymentStatus", thisPaymentStatus)
                                  .addEquals("orderStatus", thisOrderStatus)
                                  .addEquals("site", site)
                                  .addSentence("site IN (:sites)", "sites", sites)
                                  .addSentence("orderStatus in( :status )", "status", orderStatuses)
                                  .addLike("code", orderCode)
                                  .addLike("user.name", name)
                                  .addLike("user.mobile", mobile)
                                  .addLike("product.name", productName)
                                  .addDateTimeBetween("createTime", createTimeStart, createTimeEnd);
            if(orderType.isIndividual()) {
                filter.addEquals("course.experience", experience);
                filter.addEquals("course.give", give);
            }
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM Order " + getPreparedFilter(false).getWhereSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }
    /**
     * @return the name
     */
    public String getName() {
        return this.name;
    }

    /**
     * @return the orderCode
     */
    public String getOrderCode() {
        return this.orderCode;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return this.mobile;
    }

    /**
     * @return the orderType
     */
    public OrderType getOrderType() {
        return this.orderType;
    }

    /**
     * @return the experience
     */
    public boolean getExperience() {
        return this.experience;
    }

    /**
     * @return the give
     */
    public boolean getGive() {
        return this.give;
    }

    /**
     * @return the paymentStatus
     */
    public String getPaymentStatus() {
        return this.paymentStatus;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return this.status;
    }

    /**
     * @return the createTimeStart
     */
    public Date getCreateTimeStart() {
        return this.createTimeStart;
    }

    /**
     * @return the createTimeEnd
     */
    public Date getCreateTimeEnd() {
        return this.createTimeEnd;
    }

    /**
     * @return the orderStatus
     */
    public String getOrderStatus() {
        return this.orderStatus;
    }

    /**
     * @return the productName
     */
    public String getProductName() {
        return this.productName;
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param orderCode the orderCode to set
     */
    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param orderType the orderType to set
     */
    public void setOrderType(OrderType orderType) {
        this.orderType = orderType;
    }

    /**
     * @param experience the experience to set
     */
    public void setExperience(boolean experience) {
        this.experience = experience;
    }

    /**
     * @param give the give to set
     */
    public void setGive(boolean give) {
        this.give = give;
    }

    /**
     * @param paymentStatus the paymentStatus to set
     */
    public void setPaymentStatus(String paymentStatus) {
        this.paymentStatus = paymentStatus;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @param createTimeStart the createTimeStart to set
     */
    public void setCreateTimeStart(Date createTimeStart) {
        this.createTimeStart = createTimeStart;
    }

    /**
     * @param createTimeEnd the createTimeEnd to set
     */
    public void setCreateTimeEnd(Date createTimeEnd) {
        this.createTimeEnd = createTimeEnd;
    }

    /**
     * @param orderStatus the orderStatus to set
     */
    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    /**
     * @param productName the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }
}
